// 使用jquery的继承
// $.ajax

(function($){
    var loadIndex;
    $.extend({
        initTableTree:function(columns,url,id,treeShowField,parentIdField){
            $('#table').bootstrapTable({
                columns:columns,
                url:url,
                method:'post',
                toolbar:"#toolbar",
                showRefresh:true,//显示刷新按钮
                showColumns:true,
                showToggle:true,
                showFullscreen:true,
                showPaginationSwitch:true,
                contentType:'application/x-www-form-urlencoded',
                queryParamsType:'',
                idField: id,
                treeShowField:treeShowField,
                parentIdField:parentIdField,
                rootParentId:0,
                responseHandler:function(res,jqXHR){
                    // total 总条数
                    // rows 当前页数据
                    let obj=res.result;
                    return obj;
                },
                queryParams:function(params){
                    let formParams = $("#queryForm").serialize();
                    return formParams;
                },
                onPostBody: function() {
                    var columns = $("#table").bootstrapTable('getOptions').columns

                    $("#table").treegrid({
                        treeColumn: 1,
                        onChange: function() {
                            $("#table").bootstrapTable('resetView')
                        }
                    });
                }

            });
        },
        // modal:和弹出层相关的
        initTable:function(columns,url){
            // 直接写url默认使用get请求

            $('#table').bootstrapTable({
                columns:columns,
                url:url,
                method:'post',
                pagination:true,
                pageSize:15,
                pageNumber:1,
                toolbar:"#toolbar",
                showRefresh:true,//显示刷新按钮
                showColumns:true,
                showToggle:true,
                showFullscreen:true,
                showPaginationSwitch:true,
                contentType:'application/x-www-form-urlencoded',
                pageList:[10,15,16,17],
                sidePagination:'server',
                queryParamsType:'',
                onPageChange:function(num,size){
                    // 从新发请求
                    this.pageNumber=num;
                },
                responseHandler:function(res,jqXHR){
                   // total 总条数
                    // rows 当前页数据
                    let obj={total:res.result.total,rows:res.result.list};
                    return obj;
                },
                queryParams:function(params){
                    let formParams = $("#queryForm").serialize();
                    return formParams+"&pageNumber="+this.pageNumber;
                }

            });
        },
        refresh:function(){
            $("#table").bootstrapTable("refresh");
        },
        // 异步请求相关的
        operator:{
            post:function(url,data,callback){
                $.ajax({
                    url:url,
                    type:'post',
                    dataType:'json',
                    data:data,
                    beforeSend:function(){
                       loadIndex = layer.load(1); //风格1的加载
                        // $.blockUI();
                        // $('.blockOverlay').attr('title','正在加载请稍后');
                    },
                    success:function (ret) {
                        if(typeof callback =='function'){
                            callback(ret);
                        }
                        layer.close(loadIndex);
                        // $.unblockUI();
                    }
                })
            },
            get:function(url,data,callback){
                $.ajax({
                    url:url,
                    data:data,
                    dataType:'json',
                    type:'get',
                    beforeSend:function(){
                        loadIndex = layer.load(1); //风格1的加载
                    },
                    success:function(ret){
                        if(typeof callback =='function'){
                            callback(ret);
                        }
                        layer.close(loadIndex);
                    }
                })
            },
            delete:function(url,callback){
                $.ajax({
                    url:url,
                    dataType:'json',
                    type:'delete',
                    beforeSend:function(){
                        loadIndex = layer.load(1); //风格1的加载
                    },
                    success:function(ret){
                        if(typeof callback =='function'){
                            callback(ret);
                        }
                        layer.close(loadIndex);
                    }
                })
            }

        }
    })
})(jQuery)